package com.bosssoft.boss_exam_db.service.busines.impl;

import com.bosssoft.boss_exam_db.entity.Company;
import com.bosssoft.boss_exam_db.entity.QuestionType;
import com.bosssoft.boss_exam_db.mapper.QuestionTypeMapper;
import com.bosssoft.boss_exam_db.service.base.impl.BaseServiceImpl;
import com.bosssoft.boss_exam_db.service.busines.QuestionTypeService;
import com.bosssoft.boss_exam_db.util.MySqlUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class QuestionTypeServiceImpl extends BaseServiceImpl<QuestionType> implements QuestionTypeService {

    //注入QuestionTypeMapper
    @Autowired
    private QuestionTypeMapper questionTypeMapper;


    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/3/16
     * @Description 根据题型名称查询题型
     * @Param [company, questionTypeName, page, limit, sort, order] 当前用户所处公司、 题型名称 当前页数、每页条数 排序依据、排序方式
     * @Return com.github.pagehelper.PageInfo<com.bosssoft.boss_exam_db.entity.QuestionType>
     * @Exception //无
     * @since JDK 1.8
     **/
    @Override
    public PageInfo<QuestionType> queryByCondition(Company company, String questionTypeName, Integer page, Integer limit, String sort, String order) {

        Example example = new Example(QuestionType.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("companyId", company.getId());
        if (!StringUtils.isEmpty(questionTypeName)) {
            criteria.andLike("name", MySqlUtil.cloumLike(questionTypeName));
        }
        criteria.andEqualTo("deleted",false);
        PageInfo<QuestionType> groupPageInfo = PageHelper.startPage(page, limit).setOrderBy(sort + " " + order).doSelectPageInfo(() ->
                this.questionTypeMapper.selectByExample(example)
        );
        return groupPageInfo;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description  根公司ID查询公司下的题型
     * @Param [id] 公司ID
     * @Return java.util.List<com.bosssoft.boss_exam_db.entity.QuestionType>
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public List<QuestionType> queryByCompany(String id) {
        Example example = new Example(QuestionType.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("companyId",id).andEqualTo("deleted",false);
        return  questionTypeMapper.selectByExample(example);
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description
     * @Param []
     * @Return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @Exception
     * @since JDK 1.8
     **/
    // fixme 此处应该多个公司ID作为条件，遗漏了，暂时不加了
    @Override
    public List<Map<String, Object>> queryQuestionTypeMapList() {
            Example example = new Example(QuestionType.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("deleted",false);
            List<QuestionType> questionTypes = questionTypeMapper.selectByExample(example);
            ArrayList<Map<String, Object>> mapArrayList = new ArrayList<>();
            questionTypes.stream().forEach(questionType -> {
                HashMap<String, Object> map = new HashMap<>();
                map.put("id", questionType.getId());
                map.put("name", questionType.getName());
                map.put("orderNum",questionType.getOrderNum());
                mapArrayList.add(map);
            });
            return mapArrayList;
        }
}
